﻿using System;
using System.Data;

namespace EnterpriseFramework.DataAccess
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field,
                    Inherited = false, AllowMultiple = false)]
    public class ColumnAttribute : Attribute
    {
        private string name;
        private DbType dbtype;
        private int size;
        private bool primaryKey;
        private bool autoIncrement;
        private bool concurrency;
        private bool allowNull;
        private bool isComputed;
        private bool isReadOnly;
        private bool isUnique;

        /// <summary>
        /// Marks a property or a field as a column.
        /// </summary>
        public ColumnAttribute()
            : this(string.Empty)
        {
        }

        /// <summary>
        /// Marks a property or a field as a column.
        /// </summary>
        /// <param name="name">column name</param>
        public ColumnAttribute(string name)
            : this(name, DbType.Object)
        {
        }

        /// <summary>
        /// Marks a property or a field as a column.
        /// </summary>
        /// <param name="dbtype">database data type</param>
        public ColumnAttribute(DbType dbtype)
            : this(string.Empty, dbtype)
        {
        }

        /// <summary>
        /// Marks a property or a field as a column.
        /// </summary>
        /// <param name="name">column name</param>
        /// <param name="dbtype">database data type</param>
        public ColumnAttribute(string name, DbType dbtype)
            : this(name, dbtype, 0)
        {
        }

        /// <summary>
        /// Marks a property or a field as a column.
        /// </summary>
        /// <param name="dbtype">database data type</param>
        /// <param name="size">database data type size</param>
        public ColumnAttribute(DbType dbtype, int size)
            : this(string.Empty, dbtype, size)
        {
        }

        /// <summary>
        /// Marks a property or a field as a column.
        /// </summary>
        /// <param name="name">column name</param>
        /// <param name="dbtype">database data type</param>
        /// <param name="size">database data type size</param>
        public ColumnAttribute(string name, DbType dbtype, int size)
            : base()
        {
            this.name = name;
            this.dbtype = dbtype;
            this.size = size;
        }

        /// <summary>
        /// Gets or sets the name of this column.
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// Gets the database data type of this column.
        /// </summary>
        public DbType DBType
        {
            get { return dbtype; }
            set { dbtype = value; }
        }

        /// <summary>
        /// Gets or sets the size of the database data type.
        /// </summary>
        public int Size
        {
            get { return size; }
            set { size = value; }
        }

        /// <summary>
        /// Get or sets the Field Allow Null or not.
        /// </summary>
        public bool AllowNull
        {
            get { return allowNull; }
            set { allowNull = value; }
        }

        /// <summary>
        /// Get or sets if Field is Compueded or not. 
        /// </summary>
        public bool IsComputed
        {
            get { return isComputed; }
            set { isComputed = value; }
        }

        /// <summary>
        /// Get or sets if Field is Read only.
        /// </summary>
        public bool IsReadOnly
        {
            get { return isReadOnly; }
            set { isReadOnly = value; }
        }

        /// <summary>
        /// Gets or sets whether this column is a primary key column.
        /// </summary>
        public bool PrimaryKey
        {
            get { return primaryKey; }
            set
            {
                if (primaryKey)
                {
                    isUnique = true;
                    allowNull = false;
                }
                primaryKey = value;
            }
        }

        /// <summary>
        /// Shorthand alias for PrimaryKey.
        /// </summary>
        public bool PK
        {
            get { return primaryKey; }
            set
            {
                if (primaryKey)
                {
                    isUnique = true;
                    allowNull = false;
                }
                primaryKey = value;
            }
        }

        /// <summary>
        /// Gets or sets whether this column is an auto incrementing column.
        /// </summary>
        public bool AutoIncrement
        {
            get { return autoIncrement; }
            set
            {
                if (autoIncrement)
                {
                    IsReadOnly = true;
                    isUnique = true;
                }
                autoIncrement = value;
            }
        }

        /// <summary>
        /// Shorthand alias for AutoIncrement.
        /// </summary>
        public bool Auto
        {
            get { return autoIncrement; }
            set
            {
                if (autoIncrement)
                {
                    IsReadOnly = true;
                    isUnique = true;
                }
                autoIncrement = value;
            }
        }

        /// <summary>
        /// Gets or sets whether this column is used for concurrency checks.
        /// </summary>
        public bool Concurrency
        {
            get { return concurrency; }
            set { concurrency = value; }
        }


        public bool IsUnique
        {
            get { return isUnique; }
            set { isUnique = value; }
        }
    }
}
